package com.whh.tq;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;

import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.whh.tq.bean.QueryResultInfo;
import com.whh.tq.common.AndroidHelper;
import com.whh.tq.common.AsyncLoader;
import com.whh.tq.common.AsyncLoader.Callback;
import com.whh.tq.common.AsyncLoader.Execute;
import com.whh.tq.common.HttpHelper;
import com.whh.tq.common.ReqParamHelper;
import com.whh.tq.common.ResContentHelper;

public class LeftTicketResultActivity extends Activity {
	private Resources resources; 
	
	private List<QueryResultInfo> queryResultList = new ArrayList<QueryResultInfo>();
	private LeftTicketResultAdapter adapter;
	private ListView listLeftTicketResult;
	private TextView textLeftTicketResultTitle;
	private TextView textLeftTicketCount;
	private ProgressBar progressLoading;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_leftticket_result);
		init();
	}

	private void init() {
		resources = getApplicationContext().getResources();
		progressLoading = (ProgressBar)findViewById(R.id.progress_loading);
		textLeftTicketResultTitle = (TextView)findViewById(R.id.text_leftTicket_result_title);
		textLeftTicketCount = (TextView)findViewById(R.id.text_leftTicket_count);
		listLeftTicketResult = (ListView)findViewById(R.id.list_leftTicket_result);
		
		Bundle bundle = this.getIntent().getExtras();
		final String fromStation = bundle.getString("fromStation");
		final String toStation = bundle.getString("toStation");
		final String date = bundle.getString("date");
		String[] strArray=date.split("-");
		final String month = strArray[1];
		final String day = strArray[2];
		final String randCode = bundle.getString("randCode");
		final String trainCode = bundle.getString("trainCode");
		textLeftTicketResultTitle.setText(fromStation+"-"+toStation+"  ["+date+"]");
		textLeftTicketCount.setText(resources.getString(R.string.text_loading));
		
		AsyncLoader.execute(new Execute() {
			@Override
			public Object execute() {
				//由于ict参数和值每天都会变，只有自己去抓取然后填入
				List<NameValuePair> ictNvps = new ArrayList<NameValuePair>();
				ictNvps.addAll(ReqParamHelper.getDefaultLeftTicketPostData("test", "1", "1", "测试", "测试", "", "", ""));
				String ictStr = HttpHelper.getHtml(ReqParamHelper.URL_TICKETLEFT_QUERY, ictNvps);
				if(ictStr == null){
					return resources.getString(R.string.text_query_error);
				}
				String ict = ResContentHelper.parseIctContent(ictStr).get("ict");
				String ictValue = ResContentHelper.parseIctContent(ictStr).get("ictValue");
				Log.i(AndroidHelper.LOG_TAG,ict+":"+ictValue);
				List<NameValuePair> nvps = new ArrayList<NameValuePair>();
				nvps.addAll(ReqParamHelper.getDefaultLeftTicketPostData(randCode, month, day, fromStation, toStation, trainCode,ict,ictValue));
				String res = HttpHelper.getHtml(ReqParamHelper.URL_TICKETLEFT_QUERY, nvps);
				//判断是否有验证码错误等问题
				if(res == null){
					return resources.getString(R.string.text_query_error);
				}else if(res.indexOf(resources.getString(R.string.text_randCode_expired))>0){
					return resources.getString(R.string.text_randCode_expired);
				}else if(res.indexOf(resources.getString(R.string.text_randCode_wrong))>0){
					return resources.getString(R.string.text_randCode_expired);
				}else if(res.indexOf(resources.getString(R.string.text_query_error))>0){
					return resources.getString(R.string.text_query_error);
				}
				queryResultList=ResContentHelper.parseLeftTicketResContent(res);
				return null;
			}
		}, new Callback() {
			@Override
			public void callback(Object obj) {	
				if(null != obj){
					AndroidHelper.showToast(LeftTicketResultActivity.this,obj.toString());
				}else {
					if (null == adapter) {
						adapter = new LeftTicketResultAdapter();
						listLeftTicketResult.setAdapter(adapter);
					} else {
						adapter.notifyDataSetChanged();
					}					
				}
				textLeftTicketCount.setText(resources.getString(R.string.text_query_result_count).replace("XX", queryResultList.size()+""));			
				progressLoading.setVisibility(View.GONE);
			}
		});
	}

	private class LeftTicketResultAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return queryResultList.size();
		}

		@Override
		public Object getItem(int position) {
			return queryResultList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View view, ViewGroup parent) {
			view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_leftticket_result, null);
			QueryResultInfo result = queryResultList.get(position);
			TextView textTrainCode = (TextView) view.findViewById(R.id.text_trainCode);
			TextView textFromStationToStation = (TextView) view.findViewById(R.id.text_from_station_to_station);
			TextView textFromTimeToTime = (TextView) view.findViewById(R.id.text_from_time_to_time);
			TextView textUsedTime = (TextView) view.findViewById(R.id.text_used_time);
			TextView textSeat = (TextView) view.findViewById(R.id.text_seat);
			
			textTrainCode.setText(result.getTrainCode());
			textFromStationToStation.setText(result.getFromStation() + "-"+ result.getToStation());
			textFromTimeToTime.setText("[" + result.getFromTime() + "-"+ result.getToTime() + "]");
			textUsedTime.setText(result.getUsedTime());
			textSeat.setText(result.getLeftTicketSeatInfo());
			return view;
		}

	}
}
